/**
 * 
 */
package com.example.gametestproject;

import android.util.Log;
import android.graphics.PointF;
import com.xxxx.game.*;

/**
 * @author HuXiaolong
 *
 */
public class ClassicMode extends IGameMode
{
	@Override
	public void doAction(GameMatrix gm, PointF ptStart, MotionType type)
	{
		boolean canExchangeJs = false;
		int sel_sprite = this.getRowandColAtPoint(ptStart.x, ptStart.y);
		if (sel_sprite < 0 || sel_sprite >= gm.getRows() * gm.getCols()) return;
		//JewelSprite[][] jewels = gm.getJewelSprites();
		
		int exc_srpite = mLastSpriteIndex;
		if (!(sel_sprite >= 0 && sel_sprite < gm.getRows() * gm.getCols()))
		{
			mLastSpriteIndex = -1;
			return;
		}
		// check on 
		if (type == MotionType.Motion_Click)
		{
			Log.i("ClassMode", "MotionType.Motion_Click");
			if(mLastSpriteIndex != -1 && canExchange(gm, this.mLastSpriteIndex, sel_sprite))
			{
				exc_srpite = this.mLastSpriteIndex;
				canExchangeJs = true;
			}
		}
		else if (type == MotionType.Motion_Down)
		{ // move down
			Log.i("ClassMode", "MotionType.Motion_Down");
			if (sel_sprite / gm.getCols() < gm.getRows() -1 &&
					canExchange(gm, sel_sprite, sel_sprite + gm.getCols()))
			{
				exc_srpite = sel_sprite + gm.getCols();
				canExchangeJs = true;
			}
		}
		else if (type == MotionType.Motion_Up)
		{
			Log.i("ClassMode", "MotionType.Motion_Up");
			if (sel_sprite / gm.getCols() > 0 &&
					canExchange(gm, sel_sprite, sel_sprite - gm.getCols()))
			{
				exc_srpite = sel_sprite - gm.getCols();
				canExchangeJs = true;
			}
		}
		else if (type == MotionType.Motion_Left)
		{
			Log.i("ClassMode", "MotionType.Motion_Left");
			if (sel_sprite % gm.getCols() > 0 &&
					canExchange(gm, sel_sprite, sel_sprite - 1))
			{
				exc_srpite = sel_sprite - 1;
				canExchangeJs = true;
			}
		}
		else if (type == MotionType.Motion_Right)
		{
			Log.i("ClassMode", "MotionType.Motion_Right");
			if (sel_sprite % gm.getCols() < gm.getCols() - 1 &&
					canExchange(gm, sel_sprite, sel_sprite + 1))
			{
				exc_srpite = sel_sprite + 1;
				canExchangeJs = true;
			}
		}
		
		if (canExchangeJs)
		{
			if(this.mLastSpriteIndex != -1)
			{
				gm.getJewelSprites()[this.mLastSpriteIndex / gm.getCols()][this.mLastSpriteIndex % gm.getCols()].setSelected(false);
			}
			super.swapSprite(gm, exc_srpite, sel_sprite);
			//super.vanishSpites(gm);
			this.mLastSpriteIndex = -1;
			this.mComboTime = 0;
		}
		else
		{
			JewelSprite js = gm.getJewelSprites()[sel_sprite / gm.getCols()][sel_sprite % gm.getCols()];
			if (js != null && !js.isMoving() && js.getLiveMode() == 0)
			{
				if(this.mLastSpriteIndex != -1)
				{
					gm.getJewelSprites()[this.mLastSpriteIndex / gm.getCols()][this.mLastSpriteIndex % gm.getCols()].setSelected(false);
				}
				this.mLastSpriteIndex = sel_sprite;
				
				SpriteSelectionAnimation sa = new SpriteSelectionAnimation();
				js.combineAnimation(sa);
				//js.combineAnimation(new ShakeAnimation(js.getPosition(), 2, 2));
				js.addNewDrawing(sa);
				js.setSelected(true);
				Log.i("setSelected : ", js.getCol()+ " " + js.getRow());
			}
			else
			{
				this.mLastSpriteIndex = -1;
			}
			Log.i("ClassicMode", "can't exchange the two jewels");
		}
	}
}